package com.pzb.order.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.pzb.goods.domain.GdGoodsUnits;
import com.pzb.goods.service.IGdGoodsUnitsService;
import com.pzb.order.domain.OdOrder;
import com.pzb.order.service.IOdOrderService;
import com.pzb.wechat.domain.WechatUserAccount;
import com.pzb.wechat.service.IWechatUserAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pzb.order.mapper.OdOrderDetailMapper;
import com.pzb.order.domain.OdOrderDetail;
import com.pzb.order.service.IOdOrderDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单详情Service业务层处理
 * 
 * @author Mr_Wu
 * @date 2021-12-15
 */
@Service
public class OdOrderDetailServiceImpl implements IOdOrderDetailService 
{
    @Autowired
    private OdOrderDetailMapper odOrderDetailMapper;
    @Autowired
    private IOdOrderService odOrderService;
    @Autowired
    private IWechatUserAccountService userAccountService;
    @Autowired
    private IGdGoodsUnitsService goodsUnitsService;

    /**
     * 查询订单详情
     * 
     * @param id 订单详情主键
     * @return 订单详情
     */
    @Override
    public OdOrderDetail selectOdOrderDetailById(Long id)
    {
        OdOrderDetail orderDetail = odOrderDetailMapper.selectOdOrderDetailById(id);
        OdOrder order = odOrderService.selectOdOrderByOrderCode(orderDetail.getOrderCode());
        orderDetail.setOrder(order);
        return orderDetail;
    }

    /**
     * 查询订单详情列表
     * 
     * @param odOrderDetail 订单详情
     * @return 订单详情
     */
    @Override
    public List<OdOrderDetail> selectOdOrderDetailList(OdOrderDetail odOrderDetail)
    {
        return odOrderDetailMapper.selectOdOrderDetailList(odOrderDetail);
    }

    /**
     * 修改订单详情
     * 
     * @param odOrderDetail 订单详情
     * @return 结果
     */
    @Override
    @Transactional
    public int updateOdOrderDetail(OdOrderDetail odOrderDetail)
    {
        //如果是仅退款 同意后直接退款给买家 并设为已完成状态
        if("2".equals(odOrderDetail.getServiceType()) && "2".equals(odOrderDetail.getServiceStatus()) ){
            OdOrder order= odOrderService.selectOdOrderByOrderCode(odOrderDetail.getOrderCode());
            //退款给买家
            WechatUserAccount userAccount = userAccountService.selectWechatUserAccountByOpenId(order.getOpenid(),order.getExtAppid());
            BigDecimal balance = new BigDecimal(userAccount.getBalance()).add(new BigDecimal(odOrderDetail.getServiceAmount()));
            userAccount.setBalance(String.valueOf(balance));
            userAccountService.updateWechatUserAccount(userAccount);
            //设为售后已完成
            odOrderDetail.setServiceStatus("4");
        }
        //如果是退货退款  状态为完成是退款给买家
        if("1".equals(odOrderDetail.getServiceType()) && "4".equals(odOrderDetail.getServiceStatus())){
            //退款给买家
            OdOrder order= odOrderService.selectOdOrderByOrderCode(odOrderDetail.getOrderCode());
            WechatUserAccount userAccount = userAccountService.selectWechatUserAccountByOpenId(order.getOpenid(),order.getExtAppid());
            BigDecimal balance = new BigDecimal(userAccount.getBalance()).add(new BigDecimal(odOrderDetail.getServiceAmount()));
            userAccount.setBalance(String.valueOf(balance));
            userAccountService.updateWechatUserAccount(userAccount);
            //归还库存
            GdGoodsUnits goodsUnit = goodsUnitsService.selectGoodsUnitsByGoodsIdAndUnits(odOrderDetail.getGoodsId(), odOrderDetail.getGoodsUnit());
            if(goodsUnit!=null){
                goodsUnit.setStock(goodsUnit.getStock()+odOrderDetail.getQuantity());
                goodsUnitsService.updateGdGoodsUnits(goodsUnit);
            }

        }
        //如果不是驳回 清空驳回原因
        if(!"3".equals(odOrderDetail.getServiceStatus())){
            odOrderDetail.setRefuseDesc(null);
        }
        return odOrderDetailMapper.updateOdOrderDetail(odOrderDetail);
    }

    /**
     * 根据订单编号查询订单详情
     *
     * @param orderCode 订单编号
     * @return 结果
     */
    @Override
    public List<OdOrderDetail> selectOdOrderDetailByOrderCode(String orderCode) {
        return odOrderDetailMapper.selectOdOrderDetailByOrderCode(orderCode);
    }


}
